package nakedb;

/**
 * NakeDB: Reuse of two components detailed below:
 * 
 * Extension of Jeffrey's original GraphViewDemo of the prefuse library
 * which implements a generic ToolTipControl to display additional
 * informations of a node. Don't miss to be astonished by the fact that the
 * tooltip works in both displays ...
 * More meant as a base for discussion rather than a finalized approach.
 * 
 * @author <a href="http://jheer.org">jeffrey heer</a>
 * @author <a href="http://goosebumps4all.net/3all">martin dudek</a>
 * 
 */

import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;
import javax.swing.ToolTipManager;

import prefuse.Display;
import prefuse.controls.ControlAdapter;
import prefuse.util.GraphicsLib;
import prefuse.util.display.DisplayLib;
import prefuse.util.display.ItemBoundsListener;
import prefuse.visual.VisualItem;

public class GraphViewToolTipSimple extends JPanel {

  public static class FitOverviewListener implements ItemBoundsListener {
    private Rectangle2D m_bounds = new Rectangle2D.Double();

    private Rectangle2D m_temp = new Rectangle2D.Double();

    private double m_d = 15;

    public void itemBoundsChanged(Display d) {
      d.getItemBounds(m_temp);
      GraphicsLib.expand(m_temp, 25 / d.getScale());

      double dd = m_d / d.getScale();
      double xd = Math.abs(m_temp.getMinX() - m_bounds.getMinX());
      double yd = Math.abs(m_temp.getMinY() - m_bounds.getMinY());
      double wd = Math.abs(m_temp.getWidth() - m_bounds.getWidth());
      double hd = Math.abs(m_temp.getHeight() - m_bounds.getHeight());
      if (xd > dd || yd > dd || wd > dd || hd > dd) {
        m_bounds.setFrame(m_temp);
        DisplayLib.fitViewToBounds(d, m_bounds, 0);
      }
    }
  }

  /**
   * MAD Demo control which shows one way of creating a more extended tool tip.
   * Kept generic and meant as a base for individual extensions
   * 
   * @author <a href="http://goosebumps4all.net/3all">martin dudek</a>
   * 
   */

  public static class DemoControlToolTip extends ControlAdapter {

    private String[] fields;

    private String[] texts;

    private StringBuffer sbuf;

    int dismissDelay = Integer.MAX_VALUE;
    
    private boolean isActivated;

    public DemoControlToolTip(String[] texts, String[] fields) {
      this.texts = texts;
      this.fields = fields;
      sbuf = new StringBuffer();
      setOn();
      
      ToolTipManager.sharedInstance().setInitialDelay(400);

    }

    public void itemEntered(VisualItem vi, MouseEvent e) {
      //mouse over statistics pane
	      
      StatisticsPanel.setInfo(sbuf);
      
      
        String value;
        if (isActivated()) {
        	ToolTipManager.sharedInstance().setDismissDelay(dismissDelay);
        }
        sbuf.delete(0, sbuf.length());
  
        sbuf.append("<html><table>");
  
        boolean someInfo = false;
  
        for (int i = 0; i < fields.length; i++) {
          if (vi.canGetString(fields[i])) {
            value = vi.getString(fields[i]);
            if (value != null && value.length() > 0) {
              sbuf.append("<tr valign='top'><td><b>");
              sbuf.append(texts[i] + "</b></td><td>");
              sbuf.append(value.replaceAll("-", "<br>"));
              someInfo = true;
            }
            sbuf.append("</td></tr>");
          }
        }
        
        StatisticsPanel.setInfo(sbuf);

  
        if (someInfo && isActivated()) {
          sbuf.append("</table>");
          //sbuf.append("<hr size=1 width=\"97%\"><div align=\"right\"><font color=\"#999999\">esc to close&nbsp;</font></div>");
          sbuf.append("</html>");
  
          Display disp = (Display) e.getSource(); // the display on which the
                                                  // event occured
          disp.setToolTipText(sbuf.toString());
        }
      
    }

    public void itemExited(VisualItem item, MouseEvent e) {
      Display disp = (Display) e.getSource();
      disp.setToolTipText(null);
    }
    
    public void setOn() {
      isActivated = true;
    }
    
    public void setOff() {
      isActivated = false;
    }
    
    public boolean isActivated() {
      return isActivated;
    }

  } 

} 
